package edu.whu;

import java.io.File;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Properties;

public class MyClass {
    private String name;
    private String pwd;

    public MyClass(){ }
    public MyClass(String name, String pwd){
        this.name = name;
        this.pwd = pwd;
    }
    @initMethod
    public boolean init(String name, String pwd){
        if(pwd.length() >= 6 && pwd.matches("[a-zA-Z]+[0-9]+")){
            this.name = name;
            this.pwd = pwd;
            System.out.println(String.format("%s, your password is set Successfully", this.name));
            return true;
        }
        else{
            System.out.println("Illegal Pwd!");
            return false;
        }

    }


    public String getName() {
        return name;
    }

    public String getPwd() {
        return pwd;
    }
}

class Demo{
    public static void main(String[] args){
        Properties props = new Properties();
        File directory = new File("");//参数为空

        try(InputStream input = ClassLoader.getSystemResourceAsStream("myapp.properties")){

            if(input == null) return;
            props.load(input);

            String className = props.getProperty("bootstrapClass");
            Class myClass = Class.forName(className);
            Object user =  myClass.newInstance();
            Method[] methods = myClass.getMethods();
            for(Method method : methods) {
                if (method.isAnnotationPresent(initMethod.class)) {
                    method.invoke(user, "YHY", "YHY123");
                    break;
                }
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
}

